package com.baidu.sggc.service;

import com.baidu.sggc.data.resp.Page;
import com.baidu.sggc.entity.ResourceInfo;
import com.baidu.sggc.mapper.ResourceInfoMapper;
import com.baidu.sggc.core.common.utils.InputValidatorUtils;
import com.baidu.sggc.core.common.utils.MsgSenderUtil;
import lombok.extern.slf4j.Slf4j;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.baidu.sggc.entity.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.List;


/**
 * 资源信息表(ResourceInfo)表服务接口
 *
 * @author ZengFz
 * @since 2025-02-21 09:34:02
 */
@Slf4j
@Service
public class ResourceInfoService {

    @Autowired
    private ResourceInfoMapper resourceInfoMapper;

    @Autowired
    private MsgSenderUtil msgSenderUtil;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public ResourceInfo queryById(Long id) {
        return resourceInfoMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param resourceInfo 筛选条件
     * @return 查询结果
     */
    public Page<ResourceInfo> queryByPage(ResourceInfo resourceInfo) {
        // 开启分页
        PageHelper.startPage(resourceInfo.getPageNum(), resourceInfo.getPageSize());
        List<ResourceInfo> resourceInfos = resourceInfoMapper.list(resourceInfo);
        PageInfo<ResourceInfo> pageInfo = new PageInfo<>(resourceInfos);
        if (!CollectionUtils.isEmpty(resourceInfos)) {
            for (ResourceInfo info : resourceInfos) {
                info.setUserName(UserContext.getUserName());
            }
        }
        return new Page<>(pageInfo.getTotal(), resourceInfos, pageInfo.isHasNextPage());
    }

    /**
     * 新增数据
     *
     * @param resourceInfo 实例对象
     * @return 实例对象
     */
    public void insert(ResourceInfo resourceInfo) {
        if (!InputValidatorUtils.isValidName(resourceInfo.getResourceName())) {
            throw new IllegalArgumentException("名称不合法，只能包含中英文、数字、下划线，不能以下划线或数字开头");
        }
        // 设置创建人信息
        String userId = UserContext.getUserId();
        if (userId != null) {
            resourceInfo.setCreateId(userId);
        }
        resourceInfoMapper.insert(resourceInfo);
    }

    /**
     * 修改数据
     *
     * @param resourceInfo 实例对象
     * @return 实例对象
     */
    public ResourceInfo update(ResourceInfo resourceInfo) {
        // 设置更新人信息
        String userId = UserContext.getUserId();
        if (userId != null) {
            resourceInfo.setUpdateId(userId);
        }
        if (resourceInfo.getSubscribe() != null) {
            try {
                if (resourceInfo.getSubscribe() == 2) {
                    msgSenderUtil.send("资源订阅通知", "您已成功订阅该资源。");
                } else if (resourceInfo.getSubscribe() == 1) {
                    msgSenderUtil.send("资源订阅取消通知", "您已取消订阅该资源。");
                }
            } catch (IOException e) {
                log.error("发送订阅变更消息失败", e);
            }
        }
        this.resourceInfoMapper.updateResourceInfo(resourceInfo);
        return this.queryById(resourceInfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(Long id) {
        return this.resourceInfoMapper.deleteById(id) > 0;
    }

}
